package com.iflytek.exam.controller;


import com.iflytek.common.annotation.Log;
import com.iflytek.common.core.controller.BaseController;
import com.iflytek.common.core.domain.AjaxResult;
import com.iflytek.common.enums.BusinessType;
import com.iflytek.exam.domain.CreateExam;
import com.iflytek.exam.domain.ExamSet;
import com.iflytek.exam.domain.QuestionExam;
import com.iflytek.exam.service.ICreateExamService;
import com.iflytek.exam.service.IExamSetService;
import com.iflytek.exam.service.QuestionExamService;
import com.iflytek.exam.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/exam/start")
public class QuestionExamController extends BaseController {

    @Autowired
    private QuestionExamService questionExamService;

    @Autowired
    private ICreateExamService examService;

    @Autowired
    private IExamSetService examSetService;
    /**
     * 获取试卷试题
     */
    @PreAuthorize("@ss.hasPermi('exam:start:query')")
    @GetMapping(value = "/select/{examId}/{userId}")
    public AjaxResult getInfo(@PathVariable("examId") Long examId, @PathVariable("userId") Long userId ){
        CreateExam createExam = examService.selectcreateExamById(examId);
        ExamSet examSet = examSetService.selectExamSetByExamId(examId);
        if (examSet.getRetakesNum() == 0){
            return AjaxResult.warn("该试卷不允许再次重考!");
        }
        if (createExam == null){
            return AjaxResult.warn("考试不存在!");
        }else {
            String currentDateTime = DateTimeUtil.getCurrentDateTime();
            if (currentDateTime.compareTo(DateTimeUtil.formatDateTime(createExam.getStartTime())) < 0){
                return AjaxResult.warn("考试未开始!");
            }else if (currentDateTime.compareTo(DateTimeUtil.formatDateTime(createExam.getEndTime())) > 0){
                return AjaxResult.warn("考试已结束!");
            }
        }
        return AjaxResult.success(questionExamService.selectPaperQuestionById(examId,userId));
    }

    @PreAuthorize("@ss.hasPermi('exam:QuestionExam:add')")
    @Log(title = "试题和试卷中间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionExam questionExam)
    {
        return toAjax(questionExamService.insertQuestionExam(questionExam));
    }
}
